home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / pvrgjpeg / io.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  30KB  |  1,145 lines

  1. /*************************************************************
  2. Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
  3. PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
  4. Group. If you use this software, you agree to the following: This
  5. program package is purely experimental, and is licensed "as is".
  6. Permission is granted to use, modify, and distribute this program
  7. without charge for any purpose, provided this license/ disclaimer
  8. notice appears in the copies.  No warranty or maintenance is given,
  9. either expressed or implied.  In no event shall the author(s) be
  10. liable to you or a third party for any special, incidental,
  11. consequential, or other damages, arising out of the use or inability
  12. to use the program for any purpose (or the loss of data), even if we
  13. have been advised of such possibilities.  Any public reference or
  14. advertisement of this source code should refer to it as the Portable
  15. Video Research Group (PVRG) code, and not by any author(s) (or
  16. Stanford University) name.
  17. *************************************************************/
  18. /*
  19. ************************************************************
  20. io.c
  21.  
  22. This package is used to manipulate the raw image files.
  23.  
  24. There are two standards: block based, assumed to be in sizes of the
  25. DCT block, herein defined as BlockWidth and BlockHeight; and a special
  26. case, two-line-based, assumed to be of two lines per.
  27.  
  28. ************************************************************
  29. */
  30.  
  31. /*LABEL io.c */
  32.  
  33. /* Include definitions. */
  34. #include "globals.h"
  35. #ifdef SYSV
  36. #include <sys/fcntl.h>
  37. #include <sys/unistd.h>
  38. #endif
  39.  
  40.  
  41. /* Functions which are local and which are exported. */
  42.  
  43. /*PUBLIC*/
  44.  
  45. static BUFFER *MakeXBuffer();
  46. static void WriteXBuffer();
  47. static void ReadXBuffer();
  48. static void ReadResizeBuffer();
  49. static void FlushBuffer();
  50. static void BlockMoveTo();
  51. static void ReadXBound();
  52. static void WriteXBound();
  53.  
  54. static void LineMoveTo();
  55.  
  56. extern void ReadBlock();
  57. extern void WriteBlock();
  58. extern void ResizeIob();
  59. extern void RewindIob();
  60. extern void FlushIob();
  61. extern void SeekEndIob();
  62. extern void CloseIob();
  63. extern void MakeIob();
  64. extern void PrintIob();
  65. extern void InstallIob();
  66. extern void TerminateFile();
  67.  
  68. extern void ReadLine();
  69. extern void ReadPreambleLine();
  70. extern void WriteLine();
  71. extern void LineResetBuffers();
  72.  
  73.  
  74. /*PRIVATE*/
  75.  
  76. /* External variables */
  77. extern int Loud;
  78. extern int PointTransform;   /* Used for shifting the pels from the I/O */
  79. extern IMAGE *CImage;
  80. extern FRAME *CFrame;
  81. extern SCAN *CScan;
  82.  
  83. /* Internal variables. */
  84. static IOBUF *Iob=NULL;               /* Internal I/O buffer. */
  85. static int BlockWidth = BLOCKWIDTH;   /* Block width. */
  86. static int BlockHeight = BLOCKHEIGHT; /* Block height. */
  87.  
  88. /* Buffer calculation information */
  89.  
  90. #define BufferIndex(i,iobuf) (iobuf)->blist[(i)]
  91. #define TrueBufferPos(buffer) (((buffer)->currentoffs - \
  92. ((buffer)->tptr - (buffer)->bptr))/buffer->wsize)
  93.  
  94.  
  95. /*START*/
  96. /*BFUNC
  97.  
  98. MakeXBuffer() constructs a holding buffer for the stream input. It
  99. takes up a size passed into it and returns the appropriate buffer
  100. structure.
  101.  
  102. EFUNC*/
  103.  
  104. static BUFFER *MakeXBuffer(nelem,wsize)
  105.      int nelem;
  106.      int wsize;
  107. {
  108.   BEGIN("MakeXBuffer");
  109.   BUFFER *temp;
  110.  
  111.   if (!(temp = MakeStructure(BUFFER)))     /* Make structure */
  112.     {
  113.       WHEREAMI();
  114.       printf("Cannot allocate buffer structure.\n");
  115.       exit(ERROR_MEMORY);
  116.     }
  117.   temp->disable=0;                          /* Not disabled */
  118.   temp->wsize = wsize;                      /* Set up word size */
  119.   temp->size=nelem*wsize;                   /* Set up size, offset */
  120.   temp->currentoffs = 0;
  121.   temp->streamoffs = 0;
  122.   if (!(temp->space =(unsigned char *)     /* Allocate buffer space */
  123.     calloc(temp->size+1,sizeof(unsigned char))))
  124.     {
  125.       WHEREAMI();
  126.       printf("Cannot allocate buffer memory.\n");
  127.       exit(ERROR_MEMORY);
  128.     }
  129.   temp->tptr = temp->bptr = temp->space;
  130.   return(temp);
  131. }
  132.  
  133. /*BFUNC
  134.  
  135. ResizeIob() is used to resize the Iob height and width to conform to
  136. that of the CScan. This is used for the dynamic Number-of-lines
  137. rescaling.
  138.  
  139. EFUNC*/
  140.  
  141. void ResizeIob()
  142. {
  143.   BEGIN("ResizeIob");
  144.   int index;
  145.  
  146.   for(index=0;index<CScan->NumberComponents;index++)
  147.     {
  148.       CScan->Iob[index]->width = CFrame->Width[CScan->ci[index]];
  149.       CScan->Iob[index]->height = CFrame->Height[CScan->ci[index]];
  150.     }
  151. }
  152.  
  153. /*BFUNC
  154.  
  155. MakeIob() is used to create an Iob structure for use in the CScan
  156. structure. An Iob consists of several Buffer structures with some
  157. additional sizing information. The input flags set up the parameters
  158. of the stream.
  159.  
  160. EFUNC*/
  161.  
  162. void MakeIob(type,flags,wsize)
  163.      int type;
  164.      int flags;
  165.      int wsize;
  166. {
  167.   BEGIN("MakeIob");
  168.   int index,sofs;
  169.   BUFFER **current;
  170.   IOBUF *temp;
  171.  
  172.   for(index=0;index<CScan->NumberComponents;index++) /* Make IOBUF */
  173.     {                                                /* For each component */
  174.       if (!(temp = MakeStructure(IOBUF)))
  175.     {
  176.       WHEREAMI();
  177.       printf("Cannot allocate IOBUF structure.\n");
  178.       exit(ERROR_MEMORY);
  179.     }
  180.       temp->linelastdefault=(1<<(CFrame->DataPrecision-PointTransform-1));
  181.       temp->type = type;
  182.       temp->wsize = wsize;
  183.       temp->hpos=0;
  184.       temp->vpos=0;
  185.       temp->width = CFrame->Width[CScan->ci[index]];  /* Set up widthxheight */
  186.       temp->height = CFrame->Height[CScan->ci[index]];
  187.       if (CScan->NumberComponents==1)
  188.     {
  189.       temp->hor = 1;               /* For non-interleaved mode the freq */
  190.       temp->ver = 1;               /* is always 1x1 */
  191.     }
  192.       else
  193.     {
  194.       temp->hor = CFrame->hf[CScan->ci[index]];       /* and hf x vf */
  195.       temp->ver = CFrame->vf[CScan->ci[index]];
  196.     }
  197.       switch(temp->type)
  198.     {
  199.     case IOB_BLOCK:             /* BLOCK TYPE */
  200.       temp->num = temp->ver*BlockHeight;
  201.       break;
  202.     case IOB_LINE:             /* LINE TYPE */
  203.       temp->num = temp->ver + 1;
  204.       break;
  205.     default:
  206.       WHEREAMI();
  207.       printf("Illegal type specified: %d.\n",type);
  208.       exit(ERROR_BOUNDS);
  209.     }
  210.       temp->flags = flags;                            /* and also flags */
  211.       if (!(temp->blist =                             /*Set up buffer list */
  212.         (BUFFER **) calloc(temp->num,sizeof(BUFFER *))))
  213.     {
  214.       WHEREAMI();
  215.       printf("Cannot allocate Iob bufferlist.\n");
  216.       exit(ERROR_MEMORY);
  217.     }
  218.       if ((temp->file =                               /* Open file */
  219.        open(CFrame->ComponentFileName[CScan->ci[index]],
  220.         flags,UMASK)) < 0)
  221.     {
  222.       WHEREAMI();
  223.       printf("Cannot open file %s.\n",
  224.          CFrame->ComponentFileName[CScan->ci[index]]);
  225.       exit(ERROR_INIT_FILE);
  226.     }               /* Make buffer for every line of component in MDU */
  227.       for(sofs=0,current=temp->blist;current<temp->blist+temp->num;current++)
  228.     {
  229.       *current = MakeXBuffer(CFrame->BufferSize, wsize);
  230.       (*current)->streamoffs = sofs;
  231.       (*current)->iob = temp;
  232.       (*current)->data_linelast = temp->linelastdefault;
  233.       if (!temp->height || (current - temp->blist) < temp->height-1)
  234.         {
  235.           sofs += CFrame->Width[CScan->ci[index]]*wsize;
  236.         }
  237.     }
  238.       CScan->Iob[index] = temp;
  239.     }
  240. }
  241.  
  242. /*BFUNC
  243.  
  244. PrintIob() is used to print the current input buffer to the stdio
  245. stream.
  246.  
  247. EFUNC*/
  248.  
  249. void PrintIob()
  250. {
  251.   BEGIN("PrintIob");
  252.  
  253.   if (Iob)
  254.     {
  255.       printf("*** Iob ID: %x ***\n",Iob);
  256.       printf("Number of Buffers: %d  Width: %d  Height: %d\n",
  257.          Iob->num,Iob->width,Iob->height);
  258.       printf("hpos: %d  vpos: %d  hor-freq: %d  ver-freq: %d\n",
  259.          Iob->hpos,Iob->vpos,Iob->hor,Iob->ver);
  260.       printf("filed: %d  flags: %d  BufferListId: %x\n",
  261.          Iob->file,Iob->flags,Iob->blist);
  262.     }
  263.   else
  264.     {
  265.       printf("*** Iob ID: NULL ***\n");
  266.     }
  267. }
  268.  
  269. /*BFUNC
  270.  
  271. WriteXBuffer() writes out len elements from storage out to the buffer
  272. structure specified.  This is can result in a multiple of len bytes
  273. being written out depending on the element structure.
  274.  
  275. EFUNC*/
  276.  
  277. static void WriteXBuffer(len,storage,buffer)
  278.      int len;
  279.      int *storage;
  280.      BUFFER *buffer;
  281. {
  282.   BEGIN("WriteXBuffer");
  283.   int diff,wout;
  284.  
  285.   if (buffer->disable)
  286.     {
  287.       WHEREAMI();
  288.       printf("Attempting to write to disabled buffer!\n");
  289.     }
  290.   /* printf("Writing:%d bytes\n",len);*/
  291.   diff = buffer->size - (buffer->bptr - buffer->space); /* Find room left */
  292.   diff = diff/buffer->wsize;                        /* Scale by element # */
  293.   if(len > diff)
  294.     {                                     /* Put as many elems in */
  295.       WriteXBuffer(diff,storage,buffer);  /* If no room, then flush current */
  296.       FlushBuffer(buffer);                /* buffer out to disk */
  297.       len -= diff;
  298.       storage += diff;
  299.     }
  300.   switch(buffer->wsize)   /* Because of compatibility differences between */
  301.     {                     /* UNIX implementations, we are forced to do this */
  302.     case 1:               /* explicit ordering of bytes... */
  303.       while(len--)        /* Write the rest of the buffer out to the disk */
  304.     {
  305.       wout = *(storage++)<<PointTransform;
  306.     *(buffer->bptr++) = (unsigned char) wout;
  307.     }
  308.       break;
  309.     case 2:
  310.       while(len--)        /* Write the rest of the buffer out to the disk */
  311.     {
  312.       wout = *(storage++)<<PointTransform;
  313.       *(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
  314.       *(buffer->bptr++) = (unsigned char) wout&0xff;
  315.     }
  316.       break;
  317.     case 3:
  318.       while(len--)        /* Write the rest of the buffer out to the disk */
  319.     {
  320.       wout = *(storage++)<<PointTransform;
  321.       *(buffer->bptr++) = (unsigned char) (wout>>16)&0xff;
  322.       *(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
  323.       *(buffer->bptr++) = (unsigned char) wout&0xff;
  324.     }
  325.       break;
  326.     case 4:
  327.       while(len--)        /* Write the rest of the buffer out to the disk */
  328.     {
  329.       wout = *(storage++)<<PointTransform;
  330.       *(buffer->bptr++) = (unsigned char) (wout>>24)&0xff;
  331.       *(buffer->bptr++) = (unsigned char) (wout>>16)&0xff;
  332.       *(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
  333.       *(buffer->bptr++) = (unsigned char) wout&0xff;
  334.     }
  335.       break;
  336.     default:
  337.       WHEREAMI();
  338.       printf("Illegal word size in characters %d.\n",buffer->wsize);
  339.       exit(ERROR_BOUNDS);
  340.       break;
  341.     }
  342. }
  343.  
  344. /*BFUNC
  345.  
  346. ReadXBuffer() is fetches len amount of elements into storage from the
  347. buffer structure.  This may actually amount to an arbitrary number of
  348. characters depending on the word size.
  349.  
  350. EFUNC*/
  351.  
  352. static void ReadXBuffer(len,storage,buffer)
  353.      int len;
  354.      int *storage;
  355.      BUFFER *buffer;
  356. {
  357.   BEGIN("ReadXBuffer");
  358.   int i,numchars,maxelem,rin;
  359.  
  360.   if (buffer->disable)
  361.     {
  362.       for(i=0;i<len;i++) *(storage++)=buffer->data_linelast;
  363.       return;
  364.     }
  365.  
  366.   numchars = len*buffer->wsize;
  367.                                    /* The following command recurses because */
  368.                                    /* it's slightly more efficient that way */
  369.                                    /* when the probability of recursion is */
  370.                                    /* negligible. */
  371.   while (numchars > buffer->size)  /* We ask more than the buffer can handle */
  372.     {                              /* Inefficient for small buffer sizes */
  373.       maxelem = buffer->size/buffer->wsize;
  374.       ReadXBuffer(maxelem, storage, buffer); /* Split up into several reads */
  375.       storage += maxelem;
  376.       len -= maxelem;
  377.       numchars -= maxelem*buffer->wsize;
  378.     }
  379.   if(numchars > (buffer->tptr - buffer->bptr)) /* If we request > bytes */
  380.     ReadResizeBuffer(numchars,buffer);       /* Read those bytes in */
  381.   switch(buffer->wsize)                 /* Again, explicit input of bytes */
  382.     {
  383.     case 1:
  384.       while(len--)                      /* Now copy over to storage */
  385.     {
  386.       rin = (int) *(buffer->bptr++);
  387.       *(storage++) = rin >> PointTransform;
  388.     }
  389.       break;
  390.     case 2:
  391.       while(len--)                      /* Now copy over to storage */
  392.     {
  393.       rin = (((int)*(buffer->bptr++))<<8);
  394.       rin |= *(buffer->bptr++);
  395.       *(storage++) = rin >> PointTransform; 
  396.     }
  397.       break;
  398.     case 3:
  399.       while(len--)                      /* Now copy over to storage */
  400.     {
  401.       rin = (((int)*(buffer->bptr++))<<16);
  402.       rin |= (((int)*(buffer->bptr++))<<8);
  403.       rin |= (*(buffer->bptr++));
  404.       *(storage++) = rin >> PointTransform;
  405.     }
  406.       break;
  407.     case 4:
  408.       while(len--)                      /* Now copy over to storage */
  409.     {
  410.       rin = (((int)*(buffer->bptr++))<<24);
  411.       rin |= (((int)*(buffer->bptr++))<<16);
  412.       rin |= (((int)*(buffer->bptr++))<<8);
  413.       rin |= (*(buffer->bptr++));
  414.       *(storage++) = rin >> PointTransform;
  415.     }
  416.       break;
  417.     default:
  418.       WHEREAMI();
  419.       printf("Illegal word size in characters %d.\n",buffer->wsize);
  420.       exit(ERROR_BOUNDS);
  421.       break;
  422.     }
  423. #ifdef IO_DEBUG
  424.   WHEREAMI();
  425.   printf("last read: %d",*(storage-1));
  426.   printf("\n");
  427. #endif
  428. }
  429.  
  430. /*BFUNC
  431.  
  432. ReadResizeBuffer() reads len bytes from the stream and puts it
  433. into the buffer. 
  434.  
  435. EFUNC*/
  436.  
  437. static void ReadResizeBuffer(len,buffer)
  438.      int len;
  439.      BUFFER *buffer;
  440. {
  441.   BEGIN("ReadResizeBuffer");
  442.   int retval,diff,location,amount;
  443.  
  444.   diff = buffer->tptr - buffer->bptr;        /* Find out the current usage */
  445.   if (len > buffer->size-1)                  /* calculate if we can hold it */
  446.     {
  447.       WHEREAMI();
  448.       printf("Length Request Too Large.\n");
  449.       exit(ERROR_PARAMETER);
  450.     }
  451. #ifdef IO_DEBUG
  452.   printf("SPACE: %x BPTR: %x DIFF: %d\n",buffer->space,buffer->bptr,diff);
  453.   printf("ReadLseek %d\n",buffer->streamoffs+buffer->currentoffs);
  454. #endif
  455.   memcpy(buffer->space,buffer->bptr,diff);   /* Move buffer down. */
  456.   buffer->bptr = buffer->space;              /* Reset pointers. */
  457.   buffer->tptr = buffer->space + diff;
  458.  
  459.   location = buffer->streamoffs+buffer->currentoffs;
  460.   amount = buffer->size-(buffer->tptr - buffer->space);
  461.   lseek(buffer->iob->file,location,L_SET);
  462. #ifdef IO_DEBUG
  463.   printf("Read: Filed %d  Buf: %x  NBytes: %d\n",
  464.      buffer->iob->file,buffer->tptr,amount);
  465. #endif
  466.   if ((retval = read(buffer->iob->file,      /* Do the read */
  467.              buffer->tptr,
  468.              amount)) < 0)
  469.     {
  470.       WHEREAMI();
  471.       printf("Cannot Resize.\n");
  472.       exit(ERROR_READ);
  473.     }
  474. #ifdef IO_DEBUG
  475.   printf("ReadReturn numbytes %d\n",retval);
  476. #endif
  477.   buffer->tptr += retval;                   /* Alter pointers */
  478.   buffer->currentoffs += retval;
  479. }
  480.  
  481. /*BFUNC
  482.  
  483. FlushBuffer() saves the rest of the bytes in the buffer out to the
  484. disk.
  485.  
  486. EFUNC*/
  487.  
  488. static void FlushBuffer(buffer)
  489.      BUFFER *buffer;
  490. {
  491.   BEGIN("FlushBuffer");
  492.   int retval;
  493.  
  494. #ifdef IO_DEBUG
  495.   printf("WriteLseek %d\n",buffer->streamoffs+buffer->currentoffs);
  496. #endif
  497.   lseek(buffer->iob->file,buffer->streamoffs+buffer->currentoffs,L_SET);
  498.   if ((retval = write(buffer->iob->file,
  499.               buffer->space,
  500.               (buffer->bptr - buffer->space))) < 0)
  501.     {
  502.       WHEREAMI();
  503.       printf("Cannot flush buffer.\n");
  504.       exit(ERROR_WRITE);
  505.     }
  506.   buffer->currentoffs += (buffer->bptr - buffer->space);
  507.   buffer->bptr = buffer->space;
  508. }
  509.  
  510. /*BFUNC
  511.  
  512. ReadBlock() is used to get a block from the current Iob. This function
  513. returns (for the JPEG case) 64 bytes in the store integer array.  It
  514. is stored in row-major form; that is, the row index changes least
  515. rapidly.
  516.  
  517. EFUNC*/
  518.  
  519. void ReadBlock(store)
  520.      int *store;
  521. {
  522.   BEGIN("ReadBlock");
  523.   int i,voffs;
  524.  
  525.   voffs = (Iob->vpos % Iob->ver)*BlockHeight;  /* Find current v offset*/
  526. #ifdef IO_DEBUG                                
  527.   for(i=0;i<BlockHeight;i++)
  528.     {
  529.       printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
  530.     }
  531. #endif
  532.   for(i=voffs;i<voffs+BlockHeight;i++)         /* Use voffs to index into */
  533.     {                                          /* Buffer list of IOB */
  534. #ifdef IO_DEBUG
  535.       printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
  536. #endif
  537.       ReadXBound(BlockWidth,store,Iob->blist[i]);  /* get blockwidth elms */
  538.       store+=BlockWidth;                    /* Storage array & increment */
  539.     }                                       /* by blockwidth */
  540.   if ((++Iob->hpos % Iob->hor)==0)          /* Increment MDU block pos */
  541.     {
  542.       if ((++Iob->vpos % Iob->ver) == 0)
  543.     {
  544.       if (Iob->hpos < CScan->MDUWide*Iob->hor)
  545.         {
  546.           Iob->vpos -= Iob->ver;
  547.         }
  548.       else
  549.         {
  550.           Iob->hpos = 0;                /* If at end of raster width*/
  551.           BlockMoveTo();                /* Reload buffers from start */
  552.         }                               /* of next line. */
  553.     }
  554.       else
  555.     {
  556.       Iob->hpos -= Iob->hor;
  557.     }
  558.     }
  559. }
  560.  
  561. /*BFUNC
  562.  
  563. WriteBlock() writes an array of data in the integer array pointed to
  564. by store out to the driver specified by the IOB.  The integer array is
  565. stored in row-major form, that is, the first row of (8) elements, the
  566. second row of (8) elements....
  567.  
  568. EFUNC*/
  569.  
  570. void WriteBlock(store)
  571.      int *store;
  572. {
  573.   BEGIN("WriteBlock");
  574.   int i,voffs;
  575.  
  576.   voffs = (Iob->vpos % Iob->ver)*BlockHeight;  /* Find vertical buffer offs. */
  577.   for(i=voffs;i<voffs+BlockHeight;i++)
  578.     {
  579.       if (!Iob->height || (((Iob->vpos/Iob->ver)*BlockHeight + i) <
  580.                Iob->height))
  581.     {
  582.       WriteXBound(BlockWidth,store,Iob->blist[i]); /* write Block elms */
  583.       store+=BlockWidth;                   /* Iob indexed by offset */
  584.     }
  585.     }
  586.   if ((++Iob->hpos % Iob->hor)==0)             /* Increment block position */
  587.     {                                          /* in MDU. */
  588.       if ((++Iob->vpos % Iob->ver) == 0)
  589.     {
  590.       if (Iob->hpos < CScan->MDUWide*Iob->hor)
  591.         {
  592.           Iob->vpos -= Iob->ver;
  593.         }
  594.       else
  595.         {
  596.           Iob->hpos = 0;                  /* If at end of image (width) */
  597.           FlushIob();                     /* Flush current IOB and */
  598.           BlockMoveTo();                  /* Move to next lower MDU line */
  599.         }
  600.     }
  601.       else
  602.     {
  603.       Iob->hpos -= Iob->hor;
  604.     }
  605.     }
  606. }
  607.  
  608.  
  609. /*BFUNC
  610.  
  611. BlockMoveTo() is used to move to a specific vertical and horizontal
  612. location (block wise) specified by the current Iob. That means you set
  613. the current Iob parameters and then call BlockMoveTo().
  614.  
  615. EFUNC*/
  616.  
  617. static void BlockMoveTo()
  618. {
  619.   BEGIN("BlockMoveTo");
  620.   int i,vertical,horizontal;
  621.  
  622.   if (Loud > MUTE)
  623.     {
  624.       WHEREAMI();
  625.       printf("%x  Moving To [Horizontal:Vertical] [%d:%d] \n",
  626.          Iob,Iob->hpos,Iob->vpos);
  627.     }
  628.   horizontal =  Iob->hpos * BlockWidth;    /* Calculate actual */
  629.   vertical = Iob->vpos * BlockHeight;      /* Pixel position */
  630.   for(i=0;i<Iob->ver*BlockHeight;i++)
  631.     {
  632.       if (Iob->height)
  633.     {
  634.       vertical = 
  635.         ((vertical < Iob->height) ?
  636.          vertical : Iob->height-1);
  637.     }
  638.       Iob->blist[i]->tptr =                /* Reset pointer space */
  639.     Iob->blist[i]->bptr =              /* To show no contents */
  640.       Iob->blist[i]->space;
  641.       Iob->blist[i]->currentoffs = horizontal* Iob->wsize;/* reset h offset */
  642.       Iob->blist[i]->streamoffs = vertical * Iob->width *
  643.     Iob->wsize;                                       /* Reset v offset */
  644.       vertical++;
  645.     }
  646. }
  647.  
  648. /*BFUNC
  649.  
  650. RewindIob() brings all the pointers to the start of the file. The reset
  651. does not flush the buffers if writing.
  652.  
  653. EFUNC*/
  654.  
  655. void RewindIob()
  656. {
  657.   BEGIN("RewindIob");
  658.   int i;
  659.  
  660.   switch(Iob->type)
  661.     {
  662.     case IOB_BLOCK:
  663.       BlockWidth = BLOCKWIDTH;   /* Block width. */
  664.       BlockHeight = BLOCKHEIGHT; /* Block height. */
  665.       for(i=0;i<Iob->ver*BlockHeight;i++)
  666.     {
  667.       Iob->blist[i]->tptr =
  668.         Iob->blist[i]->bptr =
  669.           Iob->blist[i]->space;
  670.       Iob->blist[i]->currentoffs = 0;
  671.       Iob->blist[i]->streamoffs = i * Iob->width * Iob->wsize;
  672.     }
  673.       Iob->hpos = Iob->vpos = 0;
  674.       break;
  675.     case IOB_LINE:
  676.       Iob->linelastdefault=(1<<(CFrame->DataPrecision-PointTransform-1));
  677.       for(i= 0;i<Iob->ver+1;i++)
  678.     {
  679.       Iob->blist[i]->tptr =
  680.         Iob->blist[i]->bptr =
  681.           Iob->blist[i]->space;
  682.       Iob->blist[i]->currentoffs = 0;
  683.       if (!i)
  684.         {
  685.           Iob->blist[i]->streamoffs = 0;
  686.           Iob->blist[i]->disable=1;
  687.         }
  688.       else
  689.         {
  690.           Iob->blist[i]->streamoffs = (i-1) * Iob->width * Iob->wsize;
  691.           Iob->blist[i]->disable=0;
  692.         }
  693.       Iob->blist[i]->data_linelast = Iob->linelastdefault;
  694.     }
  695.       Iob->hpos = 0;
  696.       Iob->vpos = -1;
  697.       break;
  698.     default:
  699.       WHEREAMI();
  700.       printf("Bad IOB type: %d\n",Iob->type);
  701.       break;
  702.     }
  703. }
  704.  
  705. /*BFUNC
  706.  
  707. FlushIob() is used to flush all the buffers in the current Iob. This
  708. is done at the conclusion of a write on the current buffers.
  709.  
  710. EFUNC*/
  711.  
  712. void FlushIob()
  713. {
  714.   BEGIN("FlushIob");
  715.   int i;
  716.  
  717.   if (Loud > MUTE)
  718.     printf("IOB: %x  Flushing buffers\n",Iob);
  719.   switch(Iob->type)
  720.     {
  721.     case IOB_BLOCK:
  722.       for(i=0;i<Iob->ver*BlockHeight;i++)
  723.     FlushBuffer(Iob->blist[i]);
  724.       break;
  725.     case IOB_LINE:
  726.       Iob->blist[0]->data_linelast=Iob->linelastdefault;
  727.       for(i=1;i<Iob->ver+1;i++)
  728.     {
  729.       Iob->blist[i]->data_linelast=Iob->linelastdefault;
  730.       FlushBuffer(Iob->blist[i]);
  731.     }
  732.       break;
  733.     default:
  734.       WHEREAMI();
  735.       printf("Illegal IOB type: %d.\n",Iob->type);
  736.       break;
  737.     }
  738. }
  739.  
  740. /*BFUNC
  741.  
  742. SeekEndIob() is used to seek the end of all the buffers in the current
  743. Iob. This is done at the conclusion of a write, to avoid DNL problems.
  744.  
  745. EFUNC*/
  746.  
  747. void SeekEndIob()
  748. {
  749.   BEGIN("SeekEndIob");
  750.   int size,tsize;
  751.   static char Terminator[] = {0x80,0x00};
  752.  
  753.   size = lseek(Iob->file,0,2);
  754.   tsize = Iob->width*Iob->height*Iob->wsize;
  755.   if (size !=  tsize)
  756.     {
  757.       WHEREAMI();
  758.       printf("End not flush, making flush (actual: %d != target:%d)\n",
  759.          size,tsize);
  760.  
  761.       if (size<tsize)
  762.     {
  763.       lseek(Iob->file,tsize-1,0L);         /* Seek and terminate */
  764.       write(Iob->file,Terminator,1);
  765.     }
  766.       else if (size > tsize)
  767.     {
  768. #ifdef NOTRUNCATE
  769.       WHEREAMI();
  770.       printf("file is too large, only first %d bytes valid\n",
  771.          tsize);
  772. #else
  773.       ftruncate(Iob->file,tsize);                   /* simply truncate*/
  774. #endif
  775.     }
  776.     }
  777. }
  778.  
  779.  
  780. /*BFUNC
  781.  
  782. CloseIob() is used to close the current Iob.
  783.  
  784. EFUNC*/
  785.  
  786. void CloseIob()
  787. {
  788.   BEGIN("CloseIob");
  789.  
  790.   close(Iob->file);
  791. }
  792.  
  793. /*BFUNC
  794.  
  795. ReadXBound() reads nelem elements of information from the specified
  796. buffer.  It detects to see whether a load is necessary or not, or
  797. whether the current buffer is out of the image width bounds.
  798.  
  799. EFUNC*/
  800.  
  801. static void ReadXBound(nelem,cstore,buffer)
  802.      int nelem;
  803.      int *cstore;
  804.      BUFFER *buffer;
  805. {
  806.   BEGIN("ReadXBound");
  807.   int i,diff;
  808.  
  809.   if ((diff = buffer->iob->width - TrueBufferPos(buffer)) <= nelem)
  810.     {
  811. #ifdef IO_DEBUG
  812.       printf("ReadBound: Trailing Edge Detected. Diff: %d\n",diff);
  813. #endif
  814.       if (diff <= 0)
  815.     {
  816.       for(i=0;i<nelem;i++)       /* Pure outside bounds */
  817.         *(cstore++) = buffer->overflow;
  818.     }
  819.       else
  820.     {
  821.       ReadXBuffer(diff,cstore,buffer);
  822.       buffer->overflow = (unsigned int) cstore[diff-1];
  823.       for(i=diff;i<nelem;i++)     /* Replicate to bounds */
  824.         cstore[i] = cstore[i-1];
  825.     }
  826.     }
  827.   else
  828.     ReadXBuffer(nelem,cstore,buffer);
  829. }
  830.  
  831. /*BFUNC
  832.  
  833. WriteXBound() writes the integer array input to the buffer. It checks
  834. to see whether the bounds of the image width are exceeded, if so, the
  835. excess information is ignored.
  836.  
  837. EFUNC*/
  838.  
  839. static void WriteXBound(nelem,cstore,buffer)
  840.      int nelem;
  841.      int *cstore;
  842.      BUFFER *buffer;
  843. {
  844.   BEGIN("WriteXBound");
  845.   int diff;
  846.  
  847.   if ((diff = buffer->iob->width - TrueBufferPos(buffer)) <= nelem)
  848.     {                           /* Diff is balance to write to disk */
  849.       if (diff > 0)             /* Write balance out to disk */
  850.     WriteXBuffer(diff,cstore,buffer);
  851.     }
  852.   else                          /* If more than numberelem, then can put all */
  853.     WriteXBuffer(nelem,cstore,buffer);       /* to the buffer. */
  854. }
  855.  
  856. /*BFUNC
  857.  
  858. InstallIob() is used to install the Iob in the current scan as the
  859. real Iob.
  860.  
  861. EFUNC*/
  862.  
  863. void InstallIob(index)
  864.      int index;
  865. {
  866.   BEGIN("InstallIob");
  867.  
  868.   if (!(Iob = CScan->Iob[index]))
  869.     {
  870.       WHEREAMI();
  871.       printf("Warning, NULL Iob installed.\n");
  872.     }
  873. }
  874.  
  875.  
  876. /*BFUNC
  877.  
  878. TerminateFile() is a function that ensures that the entire file
  879. defined by the Iob is properly flush with the filesize specifications.
  880. This function is used when some fatal error occurs.
  881.  
  882. EFUNC*/
  883.  
  884.  
  885. void TerminateFile()
  886. {
  887.   BEGIN("TerminateFile");
  888.   int i,size;
  889.   static char Terminator[] = {0x80,0x00};
  890.  
  891.   if (CFrame->GlobalHeight)
  892.     {
  893.       printf("> GH:%d  GW:%d  R:%d\n",
  894.          CFrame->GlobalHeight,
  895.          CFrame->GlobalWidth,
  896.          CFrame->ResyncInterval);
  897.       for(i=0;i<CScan->NumberComponents;i++)
  898.     {
  899.       if (CScan->Iob[i])
  900.         {
  901.           printf(">> C:%d  N:%s  H:%d  W:%d  hf:%d  vf:%d\n",
  902.              CScan->ci[i],
  903.              CFrame->ComponentFileName[CScan->ci[i]],
  904.              CFrame->Height[CScan->ci[i]],
  905.              CFrame->Width[CScan->ci[i]],
  906.              CFrame->hf[CScan->ci[i]],
  907.              CFrame->vf[CScan->ci[i]]);
  908.           InstallIob(i);
  909.           FlushIob();
  910.           size = lseek(CScan->Iob[i]->file,0,2);
  911.           if (size !=
  912.           CFrame->Width[CScan->ci[i]]*CFrame->Height[CScan->ci[i]]*
  913.           CScan->Iob[i]->wsize)
  914.         {                                      /* Terminate file */
  915.           lseek(CScan->Iob[i]->file,           /* by seeking to end */
  916.             (CFrame->Width[CScan->ci[i]]*  /* And writing byte */
  917.              CFrame->Height[CScan->ci[i]]*
  918.              CScan->Iob[i]->wsize)-1,      /* Making flush with */
  919.             0L);                           /* Original size  */
  920.           write(CScan->Iob[i]->file,Terminator,1);
  921.         }
  922.         }
  923.     }
  924.     }
  925.   else
  926.     {
  927.       WHEREAMI();
  928.       printf("Unknown number of lines. Cannot flush file.\n");
  929.     }
  930. }
  931.  
  932.  
  933. /*BFUNC
  934.  
  935. ReadLine() reads in the lines required by the lossless function.  The
  936. array *store should be large enough to handle the line information
  937. read.
  938.  
  939. In total, there should be (HORIZONTALFREQUENCY+1) * nelem
  940. (VERTICALFREQUENCY+1) elements in the *store array.  This forms a
  941. matrix with each line consisting of:
  942.  
  943. [PastPredictor 1 element]  nelem* [HORIZONTALFREQUENCY elements]
  944.  
  945. And there are (VERTICALFREQUENCY+1) of such lines in the matrix:
  946.  
  947. Previous line (2**Precision-1) if beyond specifications of window
  948. Active line 1...
  949. ...
  950. Active line VERTICALFREQUENCY...
  951.  
  952.  
  953. EFUNC*/
  954.  
  955. void ReadLine(nelem,store)
  956.      int nelem;
  957.      int *store;
  958. {
  959.   BEGIN("ReadLine");
  960.   int i;
  961.  
  962.   for(i=0;i<Iob->ver+1;i++)         /* Use voffs to index into */
  963.     {                                          /* Buffer list of IOB */
  964. #ifdef IO_DEBUG
  965.       printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
  966. #endif
  967.       *(store++)=Iob->blist[i]->data_linelast;
  968.       ReadXBound(Iob->hor*nelem,store,Iob->blist[i]);
  969.       store+=Iob->hor*nelem;
  970.       Iob->blist[i]->data_linelast = *(store-1);
  971.     }
  972.   Iob->hpos += Iob->hor*nelem;
  973.   if (Iob->hpos >= CScan->MDUWide*Iob->hor)
  974.     {
  975.       Iob->vpos += Iob->ver;
  976.       Iob->hpos = 0;                /* If at end of raster width*/
  977.       LineMoveTo();                 /* Reload buffers from start */
  978.     }                               /* of next line. */
  979. }
  980.  
  981. /*BFUNC
  982.  
  983. ReadPreambleLine() reads the first line of the *store array for the
  984. WriteLine() companion command.  It reads it so that prediction can be
  985. accomplished with minimum effort and storage.
  986.  
  987. This command is executed before decoding a particular line for the
  988. prediction values; WriteLine() is called after the decoding is done.
  989.  
  990. EFUNC*/
  991.  
  992. void ReadPreambleLine(nelem,store)
  993.      int nelem;
  994.      int *store;
  995. {
  996.   BEGIN("ReadPreambleLine");
  997.   int i;
  998.   int preamblelength=1;
  999.  
  1000.   for(i=0;i<Iob->ver+1;i++)         /* Use voffs to index into */
  1001.     {                                          /* Buffer list of IOB */
  1002. #ifdef IO_DEBUG
  1003.       printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
  1004. #endif
  1005.       if (i<preamblelength)
  1006.     {
  1007.       *(store++)=Iob->blist[i]->data_linelast;
  1008.       ReadXBound(Iob->hor*nelem,store,Iob->blist[i]);
  1009.       store+=Iob->hor*nelem;
  1010.       Iob->blist[i]->data_linelast = *(store-1);
  1011.     }
  1012.       else
  1013.     {
  1014.       *(store) = Iob->blist[i]->data_linelast;
  1015.       store += Iob->hor*nelem+1;
  1016.     }
  1017.     }
  1018. }
  1019.  
  1020.  
  1021. /*BFUNC
  1022.  
  1023. WriteLine() is used to write a particular line out to the IOB.  The
  1024. line must be of the proper form in the array for this function to
  1025. work. 
  1026.  
  1027. In total, there should be (HORIZONTALFREQUENCY+1) * nelem
  1028. (VERTICALFREQUENCY+1) elements in the *store array.  This forms a
  1029. matrix with each line consisting of:
  1030.  
  1031. [PastPredictor 1 element]  nelem* [HORIZONTALFREQUENCY elements]
  1032.  
  1033. And there are (VERTICALFREQUENCY+1) of such lines in the matrix:
  1034.  
  1035. Previous line (2**Precision-1) if beyond specifications of window
  1036. Active line 1...
  1037. ...
  1038. Active line VERTICALFREQUENCY...
  1039.  
  1040.  
  1041. EFUNC*/
  1042.  
  1043. void WriteLine(nelem,store)
  1044.      int nelem;
  1045.      int *store;
  1046. {
  1047.   BEGIN("WriteLine");
  1048.   int i;
  1049.  
  1050.   store += Iob->hor*nelem+1;        /* Get rid of first line */
  1051.   for(i=1;i<Iob->ver+1;i++)         /* Use voffs to index into */
  1052.     {                               /* Buffer list of IOB */
  1053. #ifdef IO_DEBUG
  1054.       printf("WriteLine: %d  Store: %d Iobblist: %x\n",
  1055.          i,*(store+1),Iob->blist[i]);
  1056. #endif
  1057.  
  1058.       WriteXBound(Iob->hor*nelem,store+1,Iob->blist[i]);
  1059.       store+=(Iob->hor*nelem)+1;
  1060.       Iob->blist[i]->data_linelast = *(store-1);
  1061.     }
  1062.   Iob->hpos += Iob->hor*nelem;
  1063.   if (Iob->hpos >= CScan->MDUWide*Iob->hor)
  1064.     {
  1065.       Iob->vpos += Iob->ver;
  1066.       Iob->hpos = 0;                /* If at end of raster width*/
  1067.       FlushIob();                   /* Flush current IOB and */
  1068.       LineMoveTo();                 /* Reload buffers from start */
  1069.     }                               /* of next line. */
  1070. }
  1071.  
  1072. /*BFUNC
  1073.  
  1074. LineResetBuffers() resets all of the line buffers to the
  1075. (2\^DataPrecision-1) state.  The previous state is the default
  1076. prediction.  This commmand is used for resynchronization. The
  1077. implementation here does a trivial resetting.
  1078.  
  1079. EFUNC */
  1080.  
  1081. extern void LineResetBuffers()
  1082. {
  1083.   BEGIN("LineResetBuffers");
  1084.   int i;
  1085.  
  1086.   if (Iob->type!=IOB_LINE)
  1087.     {
  1088.       WHEREAMI();
  1089.       printf("Attempting to line reset a non-line buffer!\n");
  1090.       exit(ERROR_PARAMETER);
  1091.     }
  1092.   for(i=0;i<Iob->ver+1;i++)
  1093.     Iob->blist[i]->data_linelast = Iob->linelastdefault;
  1094. }
  1095.  
  1096. /*BFUNC
  1097.  
  1098. LineMoveTo() is used to move to a specific vertical and horizontal
  1099. location (line wise) specified by the current Iob. That means you set
  1100. the current Iob parameters and then call LineMoveTo().
  1101.  
  1102. EFUNC*/
  1103.  
  1104. static void LineMoveTo()
  1105. {
  1106.   BEGIN("LineMoveTo");
  1107.   int i,vertical,horizontal;
  1108.  
  1109.   if (Loud > MUTE)
  1110.     {
  1111.       WHEREAMI();
  1112.       printf("%x  Moving To [Horizontal:Vertical] [%d:%d] \n",
  1113.          Iob,Iob->hpos,Iob->vpos);
  1114.     }
  1115.   horizontal =  Iob->hpos;
  1116.   vertical = Iob->vpos;
  1117.   for(i=0;i<Iob->ver+1;i++)
  1118.     {                                     /* Reset last element read */
  1119.       if (vertical<0)
  1120.     {
  1121.       Iob->blist[i]->disable=1;
  1122.       continue;
  1123.     }
  1124.       Iob->blist[i]->disable=0;
  1125.       Iob->blist[i]->data_linelast=Iob->linelastdefault; 
  1126.       if (Iob->height)
  1127.     {
  1128.       vertical = 
  1129.         ((vertical < Iob->height) ?
  1130.          vertical : Iob->height-1);
  1131.     }
  1132.       Iob->blist[i]->tptr =                /* Reset pointer space */
  1133.     Iob->blist[i]->bptr =              /* To show no contents */
  1134.       Iob->blist[i]->space;
  1135.       Iob->blist[i]->currentoffs = horizontal* Iob->wsize; /* Reset h offset */
  1136.       Iob->blist[i]->streamoffs = vertical * Iob->width *
  1137.     Iob->wsize;                                        /* Reset v offset */
  1138.       vertical++;
  1139.     }
  1140. }
  1141.  
  1142.  
  1143.  
  1144. /*END*/
  1145.